home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.11 Nov 88 / Window Menu Source / wSample.p < prev    next >
Encoding:
Text File  |  1988-02-12  |  4.3 KB  |  186 lines  |  [TEXT/PJMM]

  1. {wSample -- the Mac User Education prog, adapted to use wMenus}
  2. { by Jim Matthews }
  3. PROGRAM wSample;
  4. {$I-}
  5.     USES
  6.         wMenu;
  7.     CONST
  8.         appleID = 128;
  9.         fileID = 129;
  10.         editID = 130;
  11.  
  12.         appleM = 1;
  13.         fileM = 2;
  14.         editM = 3;
  15.  
  16.         menuCount = 3;
  17.  
  18.         windowID = 128;
  19.  
  20.         undoCommand = 1;
  21.         cutCommand = 3;
  22.         copyCommand = 4;
  23.         pasteCommand = 5;
  24.         clearCommand = 6;
  25.  
  26.     VAR
  27.         myMenus : ARRAY[1..menuCount] OF MenuHandle;
  28.         dragRect, txRect : Rect;
  29.         textH : TEHandle;
  30.         theChar : char;
  31.         extended, doneFlag : boolean;
  32.         myEvent : EventRecord;
  33.         wRecord : WindowRecord;
  34.         myWindow : WindowPtr;
  35.         whichWindow : WindowPtr;
  36.         myMenuBar : wMenuBarHandle;
  37.  
  38. {SetUpWMenus -- read in menu templates and set up wMenuBar}
  39.     PROCEDURE SetUpWMenus;
  40.         VAR
  41.             i : integer;
  42.     BEGIN
  43.         myMenuBar := wInitMenus(myWindow);
  44.         myMenus[appleM] := GetMenu(appleID);
  45.         AddResMenu(myMenus[appleM], 'DRVR');
  46.         myMenus[fileM] := GetMenu(fileID);
  47.         myMenus[editM] := GetMenu(editID);
  48.  
  49.         FOR i := 1 TO menuCount DO
  50.             wInsertMenu(myMenuBar, myMenus[i], 0);
  51.     END; {SetUpWMenus}
  52.  
  53. {DoCommand -- handle menu commands}
  54.     PROCEDURE DoCommand (mResult : longint);
  55.         VAR
  56.             theItem, theMenu : integer;
  57.             name : Str255;
  58.             temp : integer;
  59.     BEGIN
  60.         theItem := LoWord(mResult);
  61.         theMenu := HiWord(mResult);
  62.  
  63.         CASE theMenu OF
  64.             appleID : 
  65.                 BEGIN
  66.                     GetItem(myMenus[appleM], theItem, name);
  67.                     temp := OpenDeskAcc(name);
  68.                     SetPort(myWindow);
  69.                 END; { appleID }
  70.             fileID : 
  71.                 doneflag := true;
  72.             editID : 
  73.                 BEGIN
  74.                     IF NOT SystemEdit(theItem - 1) THEN
  75.                         CASE theItem OF
  76.                             cutCommand : 
  77.                                 TECut(textH);
  78.                             copyCommand : 
  79.                                 TECopy(textH);
  80.                             pasteCommand : 
  81.                                 TEPaste(textH);
  82.                             clearCommand : 
  83.                                 TEDelete(textH);
  84.                         END; { case theItem of }
  85.                 END; { editID}
  86.             OTHERWISE
  87.                 ;
  88.         END; { case theMenu of }
  89.         wHiliteMenu(myMenuBar, 0);
  90.     END; { DoCommand }
  91.  
  92. {main program}
  93. BEGIN
  94.     InitGraf(@thePort);
  95.     InitFonts;
  96.     FlushEvents(everyEvent, 0);
  97.     InitWindows;
  98.     InitMenus;
  99.     TEInit;
  100.     InitDialogs(NIL);
  101.     InitCursor;
  102.  
  103.     WITH screenBits.bounds DO
  104.         SetRect(dragRect, 4, 24, right - 4, bottom - 4);
  105.     doneFlag := false;
  106.     myWindow := GetNewWindow(windowID, @wRecord, WindowPtr(-1));
  107.     SetPort(myWindow);
  108.     SetUpWMenus;
  109.     txRect := thePort^.portRect;
  110.     txRect.top := mBarHeight;
  111.     InsetRect(txRect, 3, 3);
  112.     textH := TENew(txRect, txRect);
  113.  
  114.     {main event loop}
  115.     REPEAT
  116.         SystemTask;
  117.         TEIdle(textH);
  118.  
  119.         IF GetNextEvent(everyEvent, myEvent) THEN
  120.             CASE myEvent.what OF
  121.                 mouseDown : 
  122.                     CASE FindWindow(myEvent.where, whichWindow) OF
  123.                         inSysWindow : 
  124.                             SystemClick(myEvent, whichWindow);
  125.                         inMenuBar : 
  126.                             IF MenuSelect(myEvent.where) <> 0 THEN
  127.                                 ;                    { handle da menus in the "real" menu bar }
  128.                         inDrag : 
  129.                             DragWindow(whichWindow, myEvent.where, dragRect);
  130.                         inContent : 
  131.                             BEGIN
  132.                                 GlobalToLocal(myEvent.where);
  133.                                 IF myEvent.where.v < mBarHeight THEN
  134.                                     BEGIN
  135.                                         IF whichWindow <> FrontWindow THEN
  136.                                             SelectWindow(whichWindow);
  137.                                         DoCommand(wMenuSelect(myMenuBar, myEvent.where));
  138.                                     END
  139.                                 ELSE
  140.                                     BEGIN
  141.                                         IF whichWindow <> FrontWindow THEN
  142.                                             SelectWindow(whichWindow)
  143.                                         ELSE
  144.                                             BEGIN
  145.                                                 extended := BitAnd(myEvent.modifiers, shiftKey) <> 0;
  146.                                                 TEClick(myEvent.where, extended, textH);
  147.                                             END; { whichWindow = FrontWindow}
  148.                                     END; { click below menu bar }
  149.                             END; { inContent }
  150.                         OTHERWISE
  151.                             ;
  152.                     END; { mouseDown }
  153.                 keyDown, autoKey : 
  154.                     BEGIN
  155.                         theChar := char(BitAnd(myEvent.message, charCodeMask));
  156.                         IF BitAnd(myEvent.modifiers, cmdKey) <> 0 THEN
  157.                             DoCommand(wMenuKey(myMenuBar, theChar))
  158.                         ELSE
  159.                             TEKey(theChar, textH);
  160.                     END; { keyDown, autoKey }
  161.                 activateEvt : 
  162.                     BEGIN
  163.                         IF BitAnd(myEvent.modifiers, activeFlag) <> 0 THEN
  164.                             BEGIN
  165.                                 TEActivate(textH);
  166.                                 DisableItem(myMenus[editM], undoCommand);
  167.                             END
  168.                         ELSE
  169.                             BEGIN
  170.                                 TEDeactivate(textH);
  171.                                 EnableItem(myMenus[editM], undoCommand);
  172.                             END; { activate/deactivate }
  173.                     END; {activateEvt }
  174.                 updateEvt : 
  175.                     BEGIN
  176.                         BeginUpdate(WindowPtr(myEvent.message));
  177.                         EraseRect(thePort^.portRect);
  178.                         wDrawMenuBar(myMenuBar);
  179.                         TEUpdate(thePort^.portRect, textH);
  180.                         EndUpdate(WindowPtr(myEvent.message));
  181.                     END; { updateEvt }
  182.                 OTHERWISE
  183.                     ;
  184.             END; { case event.what of }
  185.     UNTIL doneFlag;
  186. END.